<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">新增用户组</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                tooltip: '编辑用户组',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                color: 'error',
                tooltip: '删除用户组',
                popConfirm: {
                  title: '确定要删除该用户组吗？',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { message } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useModal } from '@/components/Modal';
  import { PageWrapper } from '@/components/Page';
  import { useI18n } from '@/hooks/web/useI18n';
  import { queryUserGroupList, deleteUserGroup } from '@/api/demo/device';
  import { columns, searchSchemas } from './account.data';
  import AccountModal from './AccountModal.vue';

  const { t } = useI18n();
  defineOptions({ name: 'UserGroupManagement' });

  const searchInfo = reactive<Recordable>({});
  const total = ref(0);

  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload, updateTableDataRecord }] = useTable({
    title: '用户组管理',
    api: async (params) => {
      // 确保分页参数正确设置
      const requestParams = {
        current: params.page,
        size: params.pageSize,
        // 确保搜索参数正确传递
        userGroupName: params.userGroupName,
        userGroupStatus: params.userGroupStatus,
      };
      const result = await queryUserGroupList(requestParams);
      if (result.code === 200) {
        total.value = result.data.total;
        return {
          items: result.data.records,
          total: result.data.total,
        };
      }
      return { items: [], total: 0 };
    },
    rowKey: 'id',
    columns,
    useSearchForm: true,
    searchInfo,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: true,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
    // 搜索表单配置
    formConfig: {
      labelWidth: 120,
      schemas: searchSchemas,
      autoSubmitOnEnter: true,
    },
  });

  // 新增用户组
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  // 编辑用户组
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  // 删除用户组
  async function handleDelete(record) {
    try {
      const response = await deleteUserGroup({ id: record.id });
      if (response.code === 200) {
        message.success('删除成功');
        reload();
      } else {
        message.error(response.msg || '删除失败');
      }
    } catch (error) {
      message.error('删除失败');
    }
  }

  // 处理成功回调
  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      updateTableDataRecord(values.id, values);
    } else {
      reload();
    }
  }
</script>
